<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/19/019
 * Time: 15:44
 */
namespace Gate\Controller;

use think\Controller;
use Workerman\Worker;
class WorkerController extends controller
{
    public $register;  //注册对象
    public $gateWay;   //gateway对象
    public $business;  //business对象

    //初始化聊天进程 把聊天信息加入到内存之中
    public function _initialize()
    {
        $this->checkEnv();
        // 标记是全局启动
        define('GLOBAL_START', 1);
        $this->init();

        //开启聊天
        Worker::runAll();
    }

    //开启聊天进程
    protected function init()
    {
        // register 服务必须是text协议
        $this->register = TpRegisterController::getInstance('text://0.0.0.0:1236');
        $this->gateWay = new TpGateWayController();
        $this->business = new TpBusinessController();
    }

    //检测环境
    protected function checkEnv() {
        ini_set('display_errors', 'on');
        if(strpos(strtolower(PHP_OS), 'win') === 0)
        {
            exit("start.php not support windows, please use start_for_win.bat\n");
        }

        // 检查扩展
        if(!extension_loaded('pcntl'))
        {
            exit("Please install pcntl extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
        }

        if(!extension_loaded('posix'))
        {
            exit("Please install posix extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
        }

    }
}